﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;

public class Player : MonoBehaviour, IReflector
{
    bool createLine = false;
    bool createLine2 = false;
    public bool canDrawLine = true;
    public GameObject myRenderer, bornEff, flyEff, attackedEff;

    [System.NonSerialized]
    public Vector3 moveDir;
   
    private float time = 0;
    public float moveSpeed = 30;

    public GameObject objCylinder, objCylinder2;
    GameObject cylinder, cylinder2;
    Animation anim;
    Vector3 defaultPos;
    Quaternion defaultRot;

    // Use this for initialization
    void Start()
    {
        anim = GetComponent<Animation>();
        defaultPos = transform.position;
        defaultRot = transform.rotation;
    }

    public void Init()
    {
        StopAllCoroutines();
        StartCoroutine(IEInitPlayer());
    }

    IEnumerator IEInitPlayer()
    {
        GameObject feiBiao = Instantiate(UIController.instance.mainUI.ReturnImage(), UIController.instance.mainUI.ReturnImage().transform.position, UIController.instance.mainUI.ReturnImage().transform.rotation, GameObject.Find("Canvas").transform);
        feiBiao.GetComponent<RectTransform>().DOLocalRotate(new Vector3(0, 0, 720), 0.8f, RotateMode.FastBeyond360);
        Vector3 screenPos = Camera.main.WorldToScreenPoint(defaultPos);
        feiBiao.GetComponent<RectTransform>().DOAnchorPos(screenPos, 0.8f);
        feiBiao.transform.DOScale(new Vector3(0.5f, 0.5f, 0.5f), 0.8f);
        yield return new WaitForSeconds(0.8f);
        Destroy(feiBiao);
        transform.position = defaultPos;
        transform.rotation = defaultRot;
        bornEff.SetActive(true);
        bornEff.GetComponent<ParticleSystem>().Play();
        myRenderer.SetActive(true);
        canDrawLine = true;
        anim.Play("idle");
        moveSpeed = 30;
        flyEff.SetActive(false);
        attackedEff.SetActive(false);
    }

    Vector3 downPosition;
    Vector3 dir;
    RaycastHit hit;
    LayerMask mask = 1 << 9;
    // Update is called once per frame
    void Update()
    {
        if (canDrawLine)
        {
            if (createLine)
            {
                dir = new Vector3((downPosition - Input.mousePosition).x, 0, (downPosition - Input.mousePosition).y);
                cylinder.transform.position = transform.position + Vector3.up * 0.5f;
                transform.LookAt(dir);
                if (Physics.Raycast(transform.position + Vector3.up * 0.5f, dir, out hit, maxDistance: 1000, layerMask: mask))
                {
                    cylinder.transform.LookAt(dir);
                    cylinder.transform.localScale = new Vector3(1, 1, Vector3.Distance(hit.point, transform.position));
                    cylinder.GetComponent<Renderer>().material.SetTextureScale("_TEX", new Vector2(1, Vector3.Distance(hit.point, transform.position)));
                    //cylinder.GetComponent<LineRenderer>().SetPositions(new Vector3[] { transform.position + Vector3.up * 0.5f, hit.point });
                    if (!createLine2)
                    {
                        cylinder2 = Instantiate(objCylinder2);
                        createLine2 = true;
                    }
                    Vector3 line2end = hit.collider.GetComponent<Wall>().GetTheOtherPoint(transform.position + Vector3.up * 0.5f, hit.point);
                    cylinder2.transform.position = hit.point;
                    cylinder2.transform.localScale = new Vector3(1, 1, 7);
                    cylinder2.GetComponent<Renderer>().material.SetTextureScale("_TEX", new Vector2(1, 7));
                    cylinder2.transform.LookAt(line2end);
                }
                else
                {
                    //cylinder.GetComponent<LineRenderer>().SetPositions(new Vector3[] { transform.position + Vector3.up * 0.5f, transform.position + dir });
                    cylinder.transform.LookAt(dir);
                    cylinder.transform.localScale = new Vector3(1, 1, 10);
                    cylinder.GetComponent<Renderer>().material.SetTextureScale("_TEX", new Vector2(1, 10));
                    if (createLine2)
                    {
                        Destroy(cylinder2);
                        createLine2 = false;
                    }
                }
            }
            if (Input.GetMouseButtonDown(0))
            {
                if (!createLine)
                {
                    downPosition = Input.mousePosition;
                    cylinder = Instantiate(objCylinder);
                    createLine = true;
                }
            }
            if (dir.magnitude > 0.1f && Input.GetMouseButtonUp(0))
            {
                flyEff.SetActive(true);
                canDrawLine = false;
                moveDir = dir;
                if (cylinder != null)
                    Destroy(cylinder);
                if (cylinder2 != null)
                    Destroy(cylinder2);
                createLine = false;
                createLine2 = false;
            }
        }
        else
        {
            if (moveSpeed >= 10)
                moveSpeed -= 0.5f;
            anim.Play("walk");
            transform.LookAt(transform.position + moveDir);
            time = time + Time.deltaTime;
            transform.position += moveDir.normalized * moveSpeed * Time.deltaTime;
        }
    }

    public void StopPlayer()
    {
        StartCoroutine(IEStopPlayer());
    }

    IEnumerator IEStopPlayer()
    {
        moveSpeed = 0;
        transform.Find("renzhe_04").gameObject.SetActive(false);
        yield return new WaitForSeconds(0.5f);
        transform.Find("renzhe_04").gameObject.SetActive(true);
        moveSpeed = 30;
    }

    public void PlayEff()
    {
        attackedEff.SetActive(true);
        attackedEff.GetComponent<ParticleSystem>().Play();
    }

    public Vector3 GetDir()
    {
        return moveDir;
    }

    public void SetDir(Vector3 rV)
    {
        moveDir = rV;
    }
}